import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import mysql.connector
from mysql.connector import Error
from PIL import Image, ImageTk
import subprocess
from RuleIntroPage import RuleIntroPage

# 数据库配置信息
DB_CONFIG = {
    'host': '111.229.122.184',
    'user': 'root',
    'password': 'Li2438881592?',  # 替换为你的MySQL密码
    'database': 'fruit_rat'  # 替换为你的数据库名
}
global step_time
step_time = 5
global y
y=0
# 连接到数据库
def connect_to_db():
    try:
        conn = mysql.connector.connect(**DB_CONFIG)
        return conn
    except Error as e:
        print(f"连接数据库失败: {e}")
        return None


# 鼠标进入按钮时改变文字颜色的函数
def on_enter(event, button):
    button.state(['!disabled', '!pressed'])
    button.state(['hover'])  # 直接设置为悬停状态，触发相关样式映射
    button.update_idletasks()

# 鼠标离开按钮时恢复文字颜色的函数
def on_leave(event, button):
    button.state(['!hover'])
    button.update_idletasks()
### 步骤 3: 修改按钮事件绑定


# 创建主窗口
root = tk.Tk()
root.title("游戏界面")

# 设置窗口为最大化
root.state('zoomed')

# 允许用户调整窗口大小
root.resizable(True, True)

# 加载背景图片
background_image = Image.open("./footage/image/background.jpg")
# 使用Image.Resampling.LANCZOS代替ANTIALIAS
background_image = background_image.resize((root.winfo_screenwidth(), root.winfo_screenheight()), Image.Resampling.LANCZOS)
background_photo = ImageTk.PhotoImage(background_image)

# 创建一个Canvas来放置背景图片
canvas = tk.Canvas(root, width=root.winfo_screenwidth(), height=root.winfo_screenheight())
canvas.pack(fill="both", expand=True)
canvas.create_image(0, 0, image=background_photo, anchor="nw")

# 创建样式对象并设置字体、默认文字颜色及相关样式属性
style = ttk.Style()
style.theme_use('clam')
style.configure('TButton', font=('Arial', 20), foreground='white', background='black')
# 使用style.map来更精确配置不同状态下的样式变化
style.map('TButton',
          foreground=[('active', '#FFC609'), ('hover', '#FFC609')],
          background=[('active', 'black'), ('hover', 'black')])

# 创建“开始游戏”按钮
button_start_game = ttk.Button(root, text="开始游戏", style='TButton')
button_start_game.place(relx=0.5, rely=0.2, anchor='center')
button_start_game.bind("<Enter>", lambda event: on_enter(event, button_start_game))
button_start_game.bind("<Leave>", lambda event: on_leave(event, button_start_game))

# 创建“规则介绍”按钮
button_rule_intro = ttk.Button(root, text="规则介绍", style='TButton')
button_rule_intro.place(relx=0.5, rely=0.5, anchor='center')
button_rule_intro.bind("<Enter>", lambda event: on_enter(event, button_rule_intro))
button_rule_intro.bind("<Leave>", lambda event: on_leave(event, button_rule_intro))

# 创建“对局记录”按钮
button_game_record = ttk.Button(root, text="对局记录", style='TButton')
button_game_record.place(relx=0.5, rely=0.8, anchor='center')
button_game_record.bind("<Enter>", lambda event: on_enter(event, button_game_record))
button_game_record.bind("<Leave>", lambda event: on_leave(event, button_game_record))

def accelerated_move(start_pos, end_pos, duration, steps):
    # 计算每个步骤的时间间隔
    step_time = duration / steps
    # 计算加速度的增量
    delta = (end_pos - start_pos) / steps
    for i in range(steps):
        current_pos = start_pos + i * delta
        yield current_pos
def do_nothing(*args):
    pass

# 移动按钮的函数
def move_button_detail(button, end_pos, duration, step):
    start_pos = 0
    steps = int(duration / step)
    step_size = 5*(end_pos - start_pos) / steps
    current_pos = start_pos
    end_pos = button.winfo_x() * -1 -y
    def update_position():
        nonlocal current_pos
        current_pos += step_size
        if (step_size > 0 and current_pos >= end_pos) or (step_size < 0 and current_pos <= end_pos):
            current_pos = end_pos
        button.place(x=current_pos)
        if current_pos != end_pos:
            root.after(step, update_position)

    update_position()


# 滑入新的页面
def slide_in_new_page(event=None):
    rule_intro_page = RuleIntroPage(root)
    start_x = root.winfo_width()  # 初始位置在屏幕右侧
    end_x = 0  # 结束位置在屏幕左侧

    current_x = start_x
    while current_x > end_x:
        current_x -= 10  # 每次移动10px
        rule_intro_page.frame.place(x=current_x, y=0)
        root.update()
        root.after(10, lambda: None)  # 10ms后移动下一次


# 下达按钮集体移动的指令
def move_button_command(event=None):
    global y
    button_width = button_start_game.winfo_width()
    print(-button_width)
    if y == 0:
        y = button_start_game.winfo_width()
    move_button_detail(button_start_game, -button_width, 1000, 10)
    move_button_detail(button_rule_intro, -button_width, 1000, 10)
    move_button_detail(button_game_record, -button_width, 1000, 10)


def start_game(event=None):
    move_button_command(event)
    # 子进程
    subprocess.run(['python', './cutorange.py'])


def rule_introduction(event=None):
    move_button_command(event)
    slide_in_new_page(event)


button_start_game.bind("<Button-1>", start_game)
button_rule_intro.bind("<Button-1>", lambda event: rule_introduction())
button_game_record.bind("<Button-1>", lambda event: move_button_command())
# 运行主循环
root.mainloop()